package com.gray.blog.controller.sys;

import com.gray.blog.arg.sys.AddSysGameArg;
import com.gray.blog.arg.sys.GetPageGameArg;
import com.gray.blog.arg.sys.UpdateGameArg;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.result.sys.GameDetailResult;
import com.gray.blog.result.sys.GetPageGameResult;
import com.gray.blog.service.front.FrontGameService;
import com.gray.blog.vo.sys.AddSysGameVO;
import com.gray.blog.vo.sys.GetPageGameVO;
import com.gray.blog.vo.sys.UpdateGameInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created  By zhoux 2019/11/19
 **/
@Slf4j
@RestController
@RequestMapping(value = "/sys/front/game")
@Api(value = "系统游戏内容", description = "系统游戏内容相关接口")
@CrossOrigin
public class SysGameController extends BaseController {

    @Autowired
    private FrontGameService frontGameService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "分页获取游戏信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GetPageGameResult> getPageGame(@RequestBody GetPageGameArg arg) {
        if (arg.isWrongParam()) {
            log.warn("FrontGameController.getPageGame param error arg:{}", arg);
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        GetPageGameVO vo = BeanUtil.copy(arg, GetPageGameVO.class);
        return frontGameService.getPageGame(vo);
    }

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "新增游戏信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult addSysGame(@RequestBody @Valid AddSysGameArg arg) {
        if (arg.isWrongParam()) {
            log.warn("FrontGameController.addSysGame param error arg:{}", arg);
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        AddSysGameVO addSysGameVO = BeanUtil.copy(arg, AddSysGameVO.class);
        return frontGameService.addSysGame(addSysGameVO);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "获取游戏信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GameDetailResult> getSysGame(@PathVariable("id") String id) {
        return frontGameService.getSysGame(id);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.DELETE)
    @IpCheckTrigger
    @ApiOperation(value = "删除游戏信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult deleteGame(@PathVariable("id") String id) {
        return frontGameService.deleteSysGame(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @IpCheckTrigger
    @ApiOperation(value = "根据id修改游戏信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult updateGame(@RequestBody @Valid UpdateGameArg arg) {
        if (arg.isWrongParam()) {
            log.warn("FrontGameController.updateGame param error arg:{}", arg);
            return new ModelResult<>(ErrorCodeEnum.PARAMS_ERROR);
        }
        UpdateGameInfoVO vo = BeanUtil.copy(arg, UpdateGameInfoVO.class);
        return frontGameService.updateGame(vo);
    }

}
